package com.sws.web.controller.ry_rongyue.wx;

import com.sws.common.annotation.Log;
import com.sws.common.core.controller.BaseController;
import com.sws.common.core.domain.AjaxResult;
import com.sws.common.enums.BusinessType;
import com.sws.wx_msg_push.service.ReservationReminderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 预约提醒控制器
 */
@RestController
@RequestMapping("/reservation/reminder")
@Api(tags = "预约提醒管理")
@Slf4j
public class ReservationReminderController extends BaseController {
    
    @Autowired
    private ReservationReminderService reminderService;
    
    /**
     * 手动触发预约提醒检查
     */

    @GetMapping("/check/{minutes}")
    @Log(title = "预约提醒检查", businessType = BusinessType.OTHER)
    @ApiOperation("手动检查未来指定分钟内的预约并发送提醒")
    public AjaxResult checkReminders(
            @PathVariable("minutes") 
            @ApiParam(value = "检查未来多少分钟内的预约", required = true, defaultValue = "30") 
            Integer minutes) {
        if (minutes == null || minutes < 1 || minutes > 120) {
            return AjaxResult.error("时间范围必须在1-120分钟之间");
        }
        
        log.info("手动触发预约提醒检查, 检查未来{}分钟内的预约", minutes);
        
        try {
            AjaxResult result = reminderService.checkAndSendReminders(minutes);
            log.info("预约提醒检查完成: {}", result);
            return result;
        } catch (Exception e) {
            log.error("预约提醒检查异常", e);
            return AjaxResult.error("预约提醒检查异常: " + e.getMessage());
        }
    }
    
    /**
     * 系统状态检查
     */
    @GetMapping("/status")
    @ApiOperation("检查预约提醒系统状态")
    public AjaxResult checkStatus() {
        log.info("检查预约提醒系统状态");
        return AjaxResult.success("预约提醒系统运行正常");
    }
    
    /**
     * 测试查询未来预约
     */
    @GetMapping("/test/{minutes}")
    @ApiOperation("测试查询未来指定分钟内的预约")
    public AjaxResult testQuery(
            @PathVariable("minutes") 
            @ApiParam(value = "查询未来多少分钟内的预约", required = true, defaultValue = "30") 
            Integer minutes) {
        if (minutes == null || minutes < 1 || minutes > 1440) {
            return AjaxResult.error("时间范围必须在1-1440分钟之间");
        }
        
        log.info("测试查询未来{}分钟内的预约", minutes);
        
        try {
            return AjaxResult.success(reminderService.getUpcomingReservations(minutes));
        } catch (Exception e) {
            log.error("测试查询预约异常", e);
            return AjaxResult.error("测试查询预约异常: " + e.getMessage());
        }
    }

    /**
     * 同步微信模板到数据库
     */
    @GetMapping("/template/sync")
    @ApiOperation("同步微信模板到数据库")
    public AjaxResult syncTemplates() {
        log.info("手动触发同步微信模板");
        try {
            return reminderService.syncTemplates();
        } catch (Exception e) {
            log.error("同步微信模板异常", e);
            return AjaxResult.error("同步微信模板异常: " + e.getMessage());
        }
    }

    /**
     * 获取模板详情
     */
    @GetMapping("/template/{templateId}")
    @ApiOperation("获取模板详情")
    public AjaxResult getTemplateDetail(@PathVariable("templateId") String templateId) {
        log.info("获取模板详情: {}", templateId);
        try {
            return reminderService.getTemplateDetail(templateId);
        } catch (Exception e) {
            log.error("获取模板详情异常", e);
            return AjaxResult.error("获取模板详情异常: " + e.getMessage());
        }
    }

    /**
     * 手动触发技师预约提醒检查
     */
    @GetMapping("/staff-check/{minutes}")
    @Log(title = "技师预约提醒检查", businessType = BusinessType.OTHER)
    @ApiOperation("手动检查未来指定分钟内的技师预约并发送提醒")
    public AjaxResult checkStaffReminders(
            @PathVariable("minutes") 
            @ApiParam(value = "检查未来多少分钟内的技师预约", required = true, defaultValue = "30") 
            Integer minutes) {
        if (minutes == null || minutes < 1 || minutes > 120) {
            return AjaxResult.error("时间范围必须在1-120分钟之间");
        }
        
        log.info("手动触发技师预约提醒检查, 检查未来{}分钟内的预约", minutes);
        
        try {
            AjaxResult result = reminderService.checkAndSendStaffReminders(minutes);
            log.info("技师预约提醒检查完成: {}", result);
            return result;
        } catch (Exception e) {
            log.error("技师预约提醒检查异常", e);
            return AjaxResult.error("技师预约提醒检查异常: " + e.getMessage());
        }
    }
    
    /**
     * 测试查询技师未来预约
     */
    @GetMapping("/staff-test/{minutes}")
    @ApiOperation("测试查询未来指定分钟内的技师预约")
    public AjaxResult testStaffQuery(
            @PathVariable("minutes") 
            @ApiParam(value = "查询未来多少分钟内的技师预约", required = true, defaultValue = "30") 
            Integer minutes) {
        if (minutes == null || minutes < 1 || minutes > 1440) {
            return AjaxResult.error("时间范围必须在1-1440分钟之间");
        }
        
        log.info("测试查询未来{}分钟内的技师预约", minutes);
        
        try {
            return AjaxResult.success(reminderService.getUpcomingStaffReservations(minutes));
        } catch (Exception e) {
            log.error("测试查询技师预约异常", e);
            return AjaxResult.error("测试查询技师预约异常: " + e.getMessage());
        }
    }
} 